﻿
/* 
 * Since 2008 - 2012
 */
package com.hh.bss.project.controller;

import static com.hh.bss.util.extjs.JsonHelper.outJson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.springframework.stereotype.Controller;

import com.hh.bss.auth.client.OnlineManager;
import com.hh.bss.auth.client.OnlineUser;
import com.hh.bss.base.BaseSpringController;
import com.hh.bss.common.ConstantDic;
import com.hh.bss.common.page.Page;
import com.hh.bss.common.page.PageRequest;
import com.hh.bss.customer.entity.CompanyCc;
import com.hh.bss.project.entity.SearchProject;
import com.hh.bss.project.entity.SearchProjectCcList;
import com.hh.bss.project.entity.SearchProjectCv;
import com.hh.bss.project.service.SearchProjectCcListService;
import com.hh.bss.project.vo.query.SearchProjectCcListQuery;
import com.hh.bss.talent.entity.Cv;
import com.hh.bss.talent.entity.CvContactDetail;
import com.hh.bss.talent.entity.CvSalary;
import com.hh.bss.talent.service.CvService;
import com.hh.bss.util.DateHelper;
import com.hh.bss.util.StringHelper;
import com.hh.bss.util.extjs.ExtJsPageHelper;
import com.hh.bss.util.extjs.ListRange;


@Controller
public class SearchProjectCcListController extends BaseSpringController{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	private SearchProjectCcListService searchProjectCcListService;
	
	private CvService cvService;
	
	private SearchProjectCcList searchProjectCcList;
	
	private static final ObjectMapper objectMapper = new ObjectMapper();;
	java.lang.Integer id = null;
	private String[] items;

	
	public void setCvServiceImpl(CvService service) {
		this.cvService = service;
	}
	/** 通过spring自动注入 */
	public void setSearchProjectCcListServiceImpl(SearchProjectCcListService service) {
		this.searchProjectCcListService = service;
	}	
	
	public void setId(java.lang.Integer val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}



	/**
	 * ExtGrid使用
	 * 列表
	 * @throws IOException
	 */
	public void list(HttpServletRequest request,HttpServletResponse response,SearchProjectCcListQuery searchProjectCcListQuery) throws IOException
	{
		OnlineManager onlineManager=super.getOnlineManager(request, response);
		if (onlineManager!=null){
			OnlineUser onlineUser=onlineManager.getOnlineUser();
			searchProjectCcListQuery.setOnlineUser(onlineUser);
		}
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		Page page = searchProjectCcListService.findPage(searchProjectCcListQuery);
		
		List<SearchProjectCcList> SearchProjectCcListlist = (List) page.getResult();
		ListRange<SearchProjectCcList> resultList = new ListRange<SearchProjectCcList>();
		resultList.setRows(SearchProjectCcListlist);
		resultList.setTotal(page.getTotalCount());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		outJson(response,resultList);
	}
	public void get(HttpServletRequest request,HttpServletResponse response,SearchProjectCcList searchProjectCcList) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			SearchProjectCcList oldSearchProjectCcList = (SearchProjectCcList)searchProjectCcListService.getById(searchProjectCcList.getId());
                         
			if(oldSearchProjectCcList!=null) result.put("data", oldSearchProjectCcList);
			result.put("success", true);
			result.put("msg", "成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}

	/**
	 * extGrid保存
	 * @throws IOException
	 */
	public void save(HttpServletRequest request,HttpServletResponse response,SearchProjectCcList searchProjectCcList) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
                        //设置创建用户和创建时间
			com.hh.bss.auth.client.OnlineManager onlineManager=super.getOnlineManager(request, response);
			if (onlineManager!=null){
					com.hh.bss.auth.client.OnlineUser onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null&&com.hh.bss.util.BeanUtils.isExistsProperty(SearchProjectCcList.class,"creator")) {
						try{com.hh.bss.util.BeanUtils.setProperty(searchProjectCcList,"creator",onlineUser.getUsername());}catch(Exception ex){}
					}
				}
							
			if (com.hh.bss.util.BeanUtils.isExistsProperty(SearchProjectCcList.class,"createDt")) {
					try{com.hh.bss.util.BeanUtils.setProperty(searchProjectCcList,"createDt",new Date());}catch(Exception ex){}
				}

			searchProjectCcListService.save(searchProjectCcList);
			result.put("success", true);
			result.put("msg", "添加成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	
	/**
	 * extGrid修改
	 * @throws IOException
	 */
	public void update(HttpServletRequest request,HttpServletResponse response,SearchProjectCcList searchProjectCcList) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
                        SearchProjectCcList oldsearchProjectCcList = (SearchProjectCcList)searchProjectCcListService.getById(searchProjectCcList.getId());
                         
			bind(request,oldsearchProjectCcList);
                        //设置修改用户和修改时间 
                        com.hh.bss.auth.client.OnlineManager onlineManager=super.getOnlineManager(request, response);
			if (onlineManager!=null){
					com.hh.bss.auth.client.OnlineUser onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null&&com.hh.bss.util.BeanUtils.isExistsProperty(SearchProjectCcList.class,"modifier")) {
						try{com.hh.bss.util.BeanUtils.setProperty(oldsearchProjectCcList,"modifier",onlineUser.getUsername());}catch(Exception ex){}
					}
				}
			
			//System.out.println(DateHelper.toString(searchProjectCcList.getCallDt(), "yyyy-MM-dd HH:mm:ss"));
							
			if (com.hh.bss.util.BeanUtils.isExistsProperty(SearchProjectCcList.class,"modifiedDt")) {
					try{com.hh.bss.util.BeanUtils.setProperty(oldsearchProjectCcList,"modifiedDt",new Date());}catch(Exception ex){}
				}

			if (!StringHelper.isEmpty(searchProjectCcList.getCallDtStr())) oldsearchProjectCcList.setCallDt(DateHelper.toDate(searchProjectCcList.getCallDtStr(), DateHelper.DEFAULT_DATETIME_FORMAT));

			searchProjectCcListService.update(oldsearchProjectCcList);
			result.put("success", true);
			result.put("msg", "修改成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	public void ccnocvlist(HttpServletRequest request,HttpServletResponse response,SearchProjectCcListQuery searchProjectCcListQuery) throws IOException
	{
		OnlineManager onlineManager=super.getOnlineManager(request, response);
		if (onlineManager!=null){
			OnlineUser onlineUser=onlineManager.getOnlineUser();
			searchProjectCcListQuery.setOnlineUser(onlineUser);
		}
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		Page page = searchProjectCcListService.findCcPage(searchProjectCcListQuery);
		
		List<CompanyCc> CompanyCclist = (List) page.getResult();
		ListRange<CompanyCc> resultList = new ListRange<CompanyCc>();
		resultList.setRows(CompanyCclist);
		resultList.setTotal(page.getTotalCount());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		outJson(response,resultList);
	}
	public void cclist(HttpServletRequest request,HttpServletResponse response,SearchProjectCcListQuery searchProjectCcListQuery) throws IOException
	{
		OnlineManager onlineManager=super.getOnlineManager(request, response);
		if (onlineManager!=null){
			OnlineUser onlineUser=onlineManager.getOnlineUser();
			searchProjectCcListQuery.setOnlineUser(onlineUser);
		}
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		Page page = searchProjectCcListService.findCcPage(searchProjectCcListQuery);
		
		List<CompanyCc> CompanyCclist = (List) page.getResult();
		ListRange<CompanyCc> resultList = new ListRange<CompanyCc>();
		resultList.setRows(CompanyCclist);
		resultList.setTotal(page.getTotalCount());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		outJson(response,resultList);
	}
	@SuppressWarnings("unchecked")
	public void cccvsave(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		SearchProjectCcList searchProjectCcList=new SearchProjectCcList();
		Cv cv=new Cv();
		SearchProjectCv searchProjectCv =new SearchProjectCv();
		//CvContactDetail cvContactDetail=null;
		CvSalary cvSalary=new CvSalary();
		
		String contact_json =request.getParameter("contact_data").toString(); //联系信息
		List<CvContactDetail> contact_list = objectMapper.readValue( contact_json, new TypeReference<List<CvContactDetail>>(){});
		//List contacts=new ArrayList();
		try
		{
			/*String mobile=request.getParameter("mobile");
			String email=request.getParameter("email");
			String phone=request.getParameter("phone");
			String QQ=request.getParameter("QQ");
			String MSN=request.getParameter("MSN");*/
			String cvId=request.getParameter("cvId");
			//String salary=request.getParameter("salary");
			bind(request,searchProjectCcList);
			bind(request,cv);
			bind(request,cvSalary);
			bind(request,searchProjectCv);
			if (!StringHelper.isEmpty(cvId)) cv.setId(StringHelper.parseInteger(cvId, 0));
			//判断人选是否增加过
			
			 //设置创建用户和创建时间
			com.hh.bss.auth.client.OnlineManager onlineManager=super.getOnlineManager(request, response);
			if (onlineManager!=null){
					com.hh.bss.auth.client.OnlineUser onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null) {
						try{com.hh.bss.util.BeanUtils.setProperty(searchProjectCcList,"creator",onlineUser.getUsername());}catch(Exception ex){}
						cv.setNameInformer(onlineUser.getUsername());
						cv.setNameDt(new Date());
						//cv.setResumeInformer(onlineUser.getUsername());
						//cv.setResumeDt(new Date());
						if (StringHelper.isEmpty(cv.getNameInformer())) {
							cv.setNameInformer(cv.getCreator());
						}
						cv.setCreator(onlineUser.getUsername());
						cv.setCreateDt(new Date());
						
						if (cv.getNameDt()==null) {
							cv.setNameDt(cv.getCreateDt());
						}
						if (cv.getResumeDt()==null) {
							cv.setNameDt(cv.getCreateDt());
						}

						cv.setCvFrom(ConstantDic.CvFrom_H);
					}
				}
		   /* if(!StringHelper.isEmpty(mobile)){
		    	cvContactDetail=new CvContactDetail();		    	
		    	cvContactDetail.setContactType(ConstantDic.ContactType_MOBILE);
		    	cvContactDetail.setNbr(mobile);
		    	cvContactDetail.setCreateDt(new Date());
		    	if (onlineManager!=null){
					com.tb.bss.auth.client.OnlineUser onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null) {
						cvContactDetail.setCreator(onlineUser.getUsername());						
					}
				}
		    	contacts.add(cvContactDetail);
		    }
		    if(!StringHelper.isEmpty(email)){
		    	cvContactDetail=new CvContactDetail();		    	
		    	cvContactDetail.setContactType(ConstantDic.ContactType_EMAIL);
		    	cvContactDetail.setNbr(email);
		    	cvContactDetail.setCreateDt(new Date());
		    	if (onlineManager!=null){
					com.tb.bss.auth.client.OnlineUser onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null) {
						cvContactDetail.setCreator(onlineUser.getUsername());						
					}
				}
		    	contacts.add(cvContactDetail);
		    }
		    if(!StringHelper.isEmpty(phone)){
		    	cvContactDetail=new CvContactDetail();		    	
		    	cvContactDetail.setContactType(ConstantDic.ContactType_TEL);
		    	cvContactDetail.setNbr(phone);
		    	cvContactDetail.setCreateDt(new Date());
		    	if (onlineManager!=null){
					com.tb.bss.auth.client.OnlineUser onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null) {
						cvContactDetail.setCreator(onlineUser.getUsername());						
					}
				}
		    	contacts.add(cvContactDetail);
		    }
		    if(!StringHelper.isEmpty(QQ)){
		    	cvContactDetail=new CvContactDetail();		    	
		    	cvContactDetail.setContactType(ConstantDic.ContactType_QQ);
		    	cvContactDetail.setNbr(QQ);
		    	cvContactDetail.setCreateDt(new Date());
		    	if (onlineManager!=null){
					com.tb.bss.auth.client.OnlineUser onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null) {
						cvContactDetail.setCreator(onlineUser.getUsername());						
					}
				}
		    	contacts.add(cvContactDetail);
		    }
		    if(!StringHelper.isEmpty(MSN)){
		    	cvContactDetail=new CvContactDetail();		    	
		    	cvContactDetail.setContactType(ConstantDic.ContactType_MSN);
		    	cvContactDetail.setNbr(MSN);
		    	cvContactDetail.setCreateDt(new Date());
		    	if (onlineManager!=null){
					com.tb.bss.auth.client.OnlineUser onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null) {
						cvContactDetail.setCreator(onlineUser.getUsername());						
					}
				}
		    	contacts.add(cvContactDetail);
		    }*/
		    //如果没有公司，部门，和岗位，则都设置为0
			if (searchProjectCv.getCompanyId()==null) searchProjectCv.setCompanyId(0);
			if (searchProjectCv.getDeptId()==null) searchProjectCv.setDeptId(0);
			if (searchProjectCv.getTitleId()==null) searchProjectCv.setTitleId(0);
                       
							
		    searchProjectCcList.setCreateDt(new Date());
		    //if (!StringHelper.isEmpty(searchProjectCcList.getCallDtStr())) {
	    	searchProjectCcList.setCallDt(DateHelper.toDate(searchProjectCcList.getCallDtStr(), DateHelper.DEFAULT_DATETIME_FORMAT));
	    	
	    	
	    	//if(searchProjectCcList.getCcResult()!=null&&(searchProjectCcList.getCcResult() == 2|| searchProjectCcList.getCcResult() == 3)){
	    	
	    	if(!StringHelper.isEmpty(cv.getCvName())){
	    		if(!(cvService.checkUser(cv,contact_list))){	    		
	    		 //if(!(searchProjectCcListService.addCcCheck(cv, contact_list, searchProjectCcList))){
	    			  searchProjectCcListService.saveCcCv(searchProjectCcList,searchProjectCv,cv,cvSalary,contact_list);
	    			  result.put("success", true);
		    		  result.put("msg", "添加成功!"); 
		    	}else{
		    		result.put("failure", true);
		    		result.put("msg", "该人选资料已经存在!");
		    	}
	    	}else{
	    		searchProjectCcListService.saveCcCv(searchProjectCcList,searchProjectCv,cv,cvSalary,contact_list);
	    		result.put("success", true);
	    		result.put("msg", "添加成功!");
	    	}
		    
		    //}
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	/**
	 * extGrid删除
	 * @throws IOException
	 */
	public void delete(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		String ids = request.getParameter("ids");
		String[] idarray = ids.split(",");
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			for (int i = 0; i < idarray.length; i++)
			{
				java.lang.Integer id = new java.lang.Integer((String)idarray[i]);
				searchProjectCcListService.removeById(id);
			}
			result.put("success", true);
			result.put("msg", "删除成功");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	
}
